package com.aube.mis.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aube.mis.dto.FeedbackDTO;
import com.aube.mis.dto.PaginationResultDTO;
import com.aube.mis.dto.ShowModelDTO;
import com.aube.mis.service.FeedbackService;
import com.aube.mis.service.ShowSettingService;
import com.aube.mis.vo.DataTableCriterias;

@Controller
@RequestMapping(value = "/feedback")
public class FeedbackController extends AbstractController {

	private static Logger log = LoggerFactory.getLogger(FeedbackController.class);

	@Resource
	FeedbackService feedbackService;
	
	@Resource
	ShowSettingService showSettingService;

	@RequestMapping(value = "/list")
	public String feedbackList(HttpServletResponse response, ModelMap map) {
		List<ShowModelDTO> modelList = showSettingService.getModelList(getShowId());
		map.put("modelList", modelList);
		return "/feedback/list";
	}

	@RequestMapping(value = "/list/data.ajax", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String feedbackData(@ModelAttribute DataTableCriterias criterias, String replyStatus) {
		PaginationResultDTO<FeedbackDTO> result = feedbackService.getFeedbackList(criterias, getShowId(), replyStatus);
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("list", result.getResultList());
		dataMap.put("total", result.getTotal());
		return this.createSuccessResponse(dataMap);
	}

	@RequestMapping(value = "/reply", method = RequestMethod.GET)
	public String feedbackReply(HttpServletResponse response, ModelMap map, String feedbackId) throws Throwable {
		FeedbackDTO feedback = feedbackService.getFeedbackDetail(getShowId(), feedbackId);
		map.put("feedback", feedback);
		return "/feedback/reply.page";
	}

	@RequestMapping(value = "/reply.ajax", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String feedbackReplyAjax(String feedbackId, String content) throws Throwable {
		feedbackService.reply(feedbackId, content);
		return this.createSuccessResponse();
	}
}
